package com.hilaver.dzmis.service.impl;

import java.util.Locale;
import java.util.Set;

import com.hilaver.dzmis.basicinfo.BiMeasureSpecification;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecificationItem;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtMsiDescription;
import com.hilaver.dzmis.codetable.CtStature;
import com.hilaver.dzmis.dao.ProductIdentificationDAO;
import com.hilaver.dzmis.product.ProductMeasureSpecificationItem;
import com.hilaver.dzmis.service.BasePDFService;
import com.hilaver.dzmis.util.StringUtils;
import com.lowagie.text.Document;
import com.lowagie.text.FontFactory;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.FontSelector;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;

public class PDFMeasureSpecificationExportServiceImpl extends BasePDFService {
	public static float CELL_HEIGHT = 16f;

	private CtServiceImpl ctService;

	private Document document;
	
	private BiMeasureSpecificationServiceImpl biMSService;
	
	public PDFMeasureSpecificationExportServiceImpl(Document document, Locale locale) {
		super.locale = locale;
		this.biMSService = new BiMeasureSpecificationServiceImpl();
		this.ctService = new CtServiceImpl();
		super.i18n = new I18nServiceImpl();
		this.document = document;
		this.setPageConfig();
	}
	
	public void exportMeasureSpecification(Integer id) throws Exception{
		BiMeasureSpecification biMS = this.biMSService.getObj(id);
		document.add(this.getHeader(biMS));
		document.add(this.getSkeletonImg(biMS));
		document.add(this.getMSItemTable(biMS));
		
		document.newPage();
	}
	
	private PdfPTable getHeader(BiMeasureSpecification biMS) throws Exception{
		float[] widthsTitle = { 0.5f, 0.2f, 0.2f, 0.3f, 0.3f, 0.3f};
		PdfPTable table = new PdfPTable(widthsTitle);
		table.setWidthPercentage(100);
		table.setSpacingBefore(1);
		//PdfPCell cellLeft = new PdfPCell();
//		cellLeft.setVerticalAlignment(Paragraph.ALIGN_LEFT);
//		cellLeft.setMinimumHeight(20);
//		cellLeft.setBorder(Rectangle.NO_BORDER);
//		cellLeft.setImage(Image.getInstance(super.realFileDir
//				+ "dzltd.png"));
		PdfPCell cell11 = getTableCellBoldBGGrey("ui.measurespecification.patronage", true, 1);
		table.addCell(cell11);
		
		PdfPCell cell12 = getTableCell(biMS.getPatronage(), true, 1);
		table.addCell(cell12);
		
		PdfPCell cell13 = getTableCellBoldBGGrey("message.edit.collar", true, 1);
		table.addCell(cell13);
		
		PdfPCell cell14 = getTableCell(this.ctService.getDescription(CtCollarType.class.getName(), biMS.getCtCollarType(), locale), true, 1);
		table.addCell(cell14);
		
		PdfPCell cell15 = getTableCellBoldBGGrey("ui.measurespecification.stature", true, 1);
		table.addCell(cell15);
		
		PdfPCell cell16 = getTableCell(this.ctService.getDescription(CtStature.class.getName(), biMS.getCtStature(), locale), true, 1);
		table.addCell(cell16);
		return table;
	}
	
	private PdfPTable getSkeletonImg(BiMeasureSpecification biMS) throws Exception{
		float[] widthsTitle = {0.5f, 0.5f};
		PdfPTable table = new PdfPTable(widthsTitle);
		table.setWidthPercentage(100);
		
		//table.addCell(getTableCell("", false, 1));
		if (biMS.getRealPhoto1() != null) {
			StringUtils.writeFile(super.realFileDir
					+ biMS.getRealPhoto1Name(), biMS.getRealPhoto1());
			Image photo1 = Image.getInstance(super.realFileDir
					+ biMS.getRealPhoto1Name());
			photo1.setWidthPercentage(0.6f);
			table.addCell(photo1);
		} else {
			table.addCell(getTableCell("", true, 1));
		}
		
		if (biMS.getRealPhoto2() != null) {
			StringUtils.writeFile(super.realFileDir
					+ biMS.getRealPhoto2Name(), biMS.getRealPhoto2());
			Image photo2 = Image.getInstance(super.realFileDir
					+ biMS.getRealPhoto2Name());
			photo2.setWidthPercentage(0.6f);
			table.addCell(photo2);
		} else {
			table.addCell(getTableCell("", true, 1));
		}
		//table.addCell(getTableCell("", false, 1));
		return table;
	}
	
	private PdfPTable getMSItemTable(BiMeasureSpecification biMS) throws Exception{
		String[] sizeNames = BiMeasureSpecificationServiceImpl.getSizeName(biMS.getCtStature());
		float[] widthsTitle;
		if("05".equals(biMS.getCtStature()) || "06".equals(biMS.getCtStature()) || "07".equals(biMS.getCtStature())){
			widthsTitle = new float[]{0.1f, 0.8f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f};
		}else if("04".equals(biMS.getCtStature()) ){
			widthsTitle = new float[]{0.1f, 0.8f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f};
		}else{
			widthsTitle = new float[]{0.1f, 0.8f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.1f, 0.2f};
		}
		PdfPTable table = new PdfPTable(widthsTitle);
		table.setWidthPercentage(100);
		
		table.addCell(getTableCellBoldBGGrey("N°", true, 1));
		table.addCell(getTableCellBoldBGGrey("ui.measurespecification.itemdesignations", true, 1));
		for(int i = 0; i < sizeNames.length; i ++){
			if(!"".equals(sizeNames[i])){
				if("05".equals(biMS.getCtStature()) || "06".equals(biMS.getCtStature()) || "07".equals(biMS.getCtStature())){
					table.addCell(getTableCellBoldBGGrey(sizeNames[i], true, 1));
				}else if("04".equals(biMS.getCtStature())){
					//do nothing
				}else{
					table.addCell(getTableCellBoldBGGrey(sizeNames[i], true, 2));
				}
			}
		}
		if("04".equals(biMS.getCtStature())){
			table.addCell(getTableCellBoldBGGrey(sizeNames[0], true, 2));
			table.addCell(getTableCellBoldBGGrey(sizeNames[3], true, 2));
			table.addCell(getTableCellBoldBGGrey(sizeNames[1], true, 2));
			table.addCell(getTableCellBoldBGGrey(sizeNames[4], true, 2));
			table.addCell(getTableCellBoldBGGrey(sizeNames[2], true, 2));
			table.addCell(getTableCellBoldBGGrey(sizeNames[5], true, 2));
		}
		table.addCell(getTableCellBoldBGGrey("ui.measurespecification.tolerance", true, 1));
		
		Set<BiMeasureSpecificationItem> pMSItems = biMS.getBiMeasureSpecificationItems();
		for (char j = 'A'; j <= 'U'; j++) {
			int hit = 0;
			for (BiMeasureSpecificationItem pMSItem : pMSItems) {
				if (String.valueOf(j).equalsIgnoreCase(pMSItem.getNumber())) {
					hit++;
					table.addCell(getTableCell(String.valueOf(j), true, 1));
					table.addCell(getTableCell(this.ctService.getDescription(
							CtMsiDescription.class.getName(), pMSItem
									.getCtMsiDescription(), locale), true, 1));
					
					if("05".equals(biMS.getCtStature()) || "06".equals(biMS.getCtStature()) || "07".equals(biMS.getCtStature())){
						table.addCell(getTableCell(pMSItem.getSize1() == null ? ""
								: pMSItem.getSize1().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize2() == null ? ""
								: pMSItem.getSize2().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize3() == null ? ""
								: pMSItem.getSize3().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize4() == null ? ""
								: pMSItem.getSize4().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize5() == null ? ""
								: pMSItem.getSize5().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize6() == null ? ""
								: pMSItem.getSize6().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize7() == null ? ""
								: pMSItem.getSize7().toString(), true, 1));
						table.addCell(getTableCell(pMSItem.getSize8() == null ? ""
								: pMSItem.getSize8().toString(), true, 1));
					}else if("01".equals(biMS.getCtStature())){
						table.addCell(getTableCell(pMSItem.getSize2() == null ? ""
								: pMSItem.getSize2().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize3() == null ? ""
								: pMSItem.getSize3().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize4() == null ? ""
								: pMSItem.getSize4().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize5() == null ? ""
								: pMSItem.getSize5().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize6() == null ? ""
								: pMSItem.getSize6().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
					}else if("04".equals(biMS.getCtStature())){
						table.addCell(getTableCell(pMSItem.getSize1() == null ? ""
								: pMSItem.getSize1().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize4() == null ? ""
								: pMSItem.getSize4().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						
						table.addCell(getTableCell(pMSItem.getSize2() == null ? ""
								: pMSItem.getSize2().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize5() == null ? ""
								: pMSItem.getSize5().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						
						table.addCell(getTableCell(pMSItem.getSize3() == null ? ""
								: pMSItem.getSize3().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize6() == null ? ""
								: pMSItem.getSize6().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
					}else{
						table.addCell(getTableCell(pMSItem.getSize1() == null ? ""
								: pMSItem.getSize1().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize2() == null ? ""
								: pMSItem.getSize2().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize3() == null ? ""
								: pMSItem.getSize3().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize4() == null ? ""
								: pMSItem.getSize4().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
						table.addCell(getTableCell(pMSItem.getSize5() == null ? ""
								: pMSItem.getSize5().toString(), true, 1));
						table.addCell(getTableCell("", true, 1));
					}
					table.addCell(getTableCell(
							pMSItem.getTolerance() == null ? ""
									: ("+/-" + pMSItem.getTolerance()
											.toString()), true, 1));
				}
			}
		}
		
		return table;
	}
	
	public void setRealFileDir(String realFileDir) {
		this.realFileDir = realFileDir;
	}
	
	private void setPageConfig() {
		this.document.setPageSize(PageSize.A4);
		this.document.setMargins(10, 10, 35, 35);
		HeaderFooter header = new HeaderFooter(new Phrase("Design Zone"), false);
		header.setBorder(Rectangle.NO_BORDER);
		header.setAlignment(Paragraph.ALIGN_RIGHT);
		header.setRight(140);
		this.document.setHeader(header);
		this.selector = new FontSelector();
		// selector.addFont(FontFactory.getFont(FontFactory.COURIER, 10));
		selector.addFont(FontFactory.getFont("Arial", 8));
		selector.addFont(FontFactory.getFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED, 9));
		this.boldSelector = new FontSelector();
		boldSelector.addFont(FontFactory.getFont("Arial", 8));
		boldSelector.addFont(FontFactory.getFont("STSong-Light", "UniGB-UCS2-H",
				BaseFont.NOT_EMBEDDED, 9));
	}
	
	public static String pdfFileName(Integer id){
		ProductIdentificationDAO dao = new ProductIdentificationDAO();
		CtServiceImpl ctService = new CtServiceImpl();
		String pdfName = "";
		try {
			BiMeasureSpecification biMS = (BiMeasureSpecification)dao.get(
					BiMeasureSpecification.class.getName(), id);
			
			pdfName = biMS.getPatronage();
			
			return  pdfName;
		} catch (Exception e) {
			e.printStackTrace();
			pdfName = "MSoutput";
		}
		return pdfName;
	}
}
